package com.blog.service.impl;

import com.blog.mapper.DashboardMapper;
import com.blog.pojo.AdminDashboardVO;
import com.blog.pojo.UserDashboardVO;
import com.blog.service.DashboardService;
import com.blog.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DashboardServiceImpl implements DashboardService {
    @Autowired
    private DashboardMapper dashboardMapper;

    // 管理员数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdminDashboardVO listAdminData() {
        AdminDashboardVO adminDashboardVO = new AdminDashboardVO();
        adminDashboardVO.setArticleCount(dashboardMapper.countArticle());
        adminDashboardVO.setCommentCount(dashboardMapper.countComment());
        adminDashboardVO.setUserCount(dashboardMapper.countUser());
        adminDashboardVO.setNewArticleCount(dashboardMapper.countNewArticlesThisWeek());
        adminDashboardVO.setNewCommentCount(dashboardMapper.countNewCommentsThisWeek());
        adminDashboardVO.setNewUserCount(dashboardMapper.countNewUsersThisWeek());
        adminDashboardVO.setTagArticleCountList(dashboardMapper.countArticlesByTag());
        return adminDashboardVO;
    }

    // 用户数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserDashboardVO listUserData() {
        UserDashboardVO userDashboardVO = new UserDashboardVO();
        Integer userId = Integer.parseInt(UserContext.getUserId());
        userDashboardVO.setArticleCount(dashboardMapper.countUserArticle(userId));
        userDashboardVO.setCommentCount(dashboardMapper.countUserComment(userId));
        userDashboardVO.setFollowerCount(dashboardMapper.countFollower(userId));
        userDashboardVO.setSupportCount(dashboardMapper.sumSupport(userId));
        userDashboardVO.setNewArticleCount(dashboardMapper.countUserNewArticlesThisWeek(userId));
        userDashboardVO.setNewCommentCount(dashboardMapper.countUserNewCommentsThisWeek(userId));
        return userDashboardVO;
    }

}
